import React, { forwardRef } from 'react';
import { classNames } from '../../utils';
import styles from './index.module.less';

// 按钮类型
export type ButtonType = 'default' | 'primary' | 'success' | 'warning' | 'error' | 'dashed' | 'text' | 'link';

// 按钮大小
export type ButtonSize = 'small' | 'medium' | 'large';

// 按钮形状
export type ButtonShape = 'default' | 'circle' | 'round';

// 按钮属性接口
export interface ButtonProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'type'> {
  /** 按钮类型 */
  type?: ButtonType;
  /** 按钮大小 */
  size?: ButtonSize;
  /** 按钮形状 */
  shape?: ButtonShape;
  /** 是否禁用 */
  disabled?: boolean;
  /** 是否加载中 */
  loading?: boolean;
  /** 是否块级元素 */
  block?: boolean;
  /** 图标 */
  icon?: React.ReactNode;
  /** 子元素 */
  children?: React.ReactNode;
}

/**
 * Button 组件
 * 提供丰富的按钮样式和交互效果
 */
const Button = forwardRef<HTMLButtonElement, ButtonProps>(
  (
    {
      type = 'default',
      size = 'medium',
      shape = 'default',
      disabled = false,
      loading = false,
      block = false,
      icon,
      children,
      className,
      ...rest
    },
    ref
  ) => {
    const classes = classNames(
      styles.button,
      styles[`button-${type}`],
      styles[`button-${size}`],
      styles[`button-${shape}`],
      {
        [styles['button-disabled']]: disabled,
        [styles['button-loading']]: loading,
        [styles['button-block']]: block,
        [styles['button-icon-only']]: icon && !children,
      },
      className
    );

    return (
      <button
        ref={ref}
        className={classes}
        disabled={disabled || loading}
        type="button"
        {...rest}
      >
        {loading && (
          <span className={styles['button-loading-icon']}>
            <svg viewBox="0 0 1024 1024" focusable="false" data-icon="loading" fill="currentColor">
              <path d="M988 548c-19.9 0-36-16.1-36-36 0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 00-94.3-139.9 437.71 437.71 0 00-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.3C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3.1 19.9-16 36-35.9 36z" />
            </svg>
          </span>
        )}
        {icon && !loading && <span className={styles['button-icon']}>{icon}</span>}
        {children && <span className={styles['button-content']}>{children}</span>}
      </button>
    );
  }
);

Button.displayName = 'Button';

export default Button;